package com.etcsoft.pagedesigner.dialog;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import com.etcsoft.pagedesigner.common.Log;
import com.etcsoft.pagedesigner.common.Messages;
import com.etcsoft.pagedesigner.model.ProjectItem;
import com.etcsoft.pagedesigner.model.ProjectManager;
import com.etcsoft.pagedesigner.view.ProjectContentProvider;
import com.etcsoft.pagedesigner.view.ProjectFilter;
import com.etcsoft.pagedesigner.view.ProjectLabelProvider;
import com.etcsoft.pagedesigner.view.ProjectSorter;

public class FolderOfProjectDialog extends Dialog {
    private TreeViewer viewer;
    private String projectName;
    private String parentFolerPath;

    /**
     * Create the dialog.
     * @param parentShell
     * @param projectName
     */
    public FolderOfProjectDialog(Shell parentShell, String projectName) {
        super(parentShell);
        this.projectName = projectName;
    }

    /**
     * Create contents of the dialog.
     * @param parent
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        Composite container = (Composite) super.createDialogArea(parent);
        container.setLayout(new FillLayout(SWT.HORIZONTAL));

        this.viewer = new TreeViewer(container, SWT.BORDER);
        this.viewer.setContentProvider(new ProjectContentProvider());
        this.viewer.setLabelProvider(new ProjectLabelProvider());
        this.viewer.setSorter(new ProjectSorter());
        this.viewer.addFilter(new ProjectFilter(this.viewer, this.projectName));
        this.viewer.setInput(ProjectManager.getManager());

        return container;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.dialogs.Dialog#okPressed()
     */
    @Override
    protected void okPressed() {
        IStructuredSelection selection = (IStructuredSelection) this.viewer.getSelection();
        ProjectItem projectItem = (ProjectItem) selection.getFirstElement();
        this.parentFolerPath = projectItem.getResource().getFullPath().toString().replace("/" + this.projectName + "/", "");

        Log.debug("parentFolerPath = " + this.parentFolerPath);

        super.okPressed();
    }

    /**
     * Create contents of the button bar.
     * @param parent
     */
    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        this.createButton(parent, IDialogConstants.OK_ID, Messages.DESC_BUTTON_OK,
                false);
        this.createButton(parent, IDialogConstants.CANCEL_ID,
                Messages.DESC_BUTTON_CANCEL, true);
    }

    /**
     * Return the initial size of the dialog.
     */
    @Override
    protected Point getInitialSize() {
        return new Point(450, 300);
    }

    public String getSelectParentFolder() {
        return this.parentFolerPath;
    }

}
